﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;
using System.Data.Linq;

namespace Avatar.Company
{
    public static class JobManager
    {
        public static void JobSave(
            string _title,
            int _indastryID,
            Guid _jobID,
            int _positionID,
            string _positionName,
            int _jobTypeID,
            string _userName,
            string _detail,
            int _diplomaID,
            int _languageID,
            int _languageAbilityID,
            int _locationID,
            int _number,
            int _periodID,
            int _salaryID,
            int _sexID,
            int _workingAgeID
            )
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_userName)) throw new Exception("用户名不能为空");
                if (string.IsNullOrEmpty(_title)) throw new Exception("招聘信息标题不能为空");
                if (string.IsNullOrEmpty(_positionName)) throw new Exception("职位名称不能为空");
                if (string.IsNullOrEmpty(_detail)) throw new Exception("招聘详细内容不能为空");
                if (_title.Length > 40) throw new Exception("招聘信息标题不能超过40个字符");
                if (_positionName.Length > 20) throw new Exception("职位名称不能超过40个字符");
                if (_detail.Length > 500) throw new Exception("招聘详细内容不能超过500字");

                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _j = _adc.Job.Where(j => j.ID == _jobID).FirstOrDefault();

                    if (_j == null)
                    {
                        _j = new Job();

                        _j.ID = Guid.NewGuid();
                        _j.PublisherUser = _userName;
                        _j.Title = _title;
                        _j.IndustryID = _indastryID;
                        _j.Detail = _detail;
                        _j.DiplomaID = _diplomaID;
                        _j.JobTypeID = _jobTypeID;
                        if (_languageAbilityID != 0)
                        {
                            _j.LanguageID = _languageID;
                            _j.LanguageAbilityID = _languageAbilityID;
                        }
                        _j.LocationID = _locationID;
                        _j.Number = _number;
                        _j.PeriodID = _periodID;
                        _j.PositionID = _positionID;
                        _j.PositionName = _positionName;
                        _j.SalaryID = _salaryID;
                        if (_sexID != 0)
                        {
                            _j.SexID = _sexID;
                        }
                        _j.WorkingAgeID = _workingAgeID;
                        _j.PublishDate = DateTime.Now;
                        _j.StatusID = 1;

                        _adc.Job.InsertOnSubmit(_j);
                    }
                    else
                    {
                        _j.Title = _title;
                        _j.IndustryID = _indastryID;
                        _j.Detail = _detail;
                        _j.DiplomaID = _diplomaID;
                        if (_languageAbilityID != 0)
                        {
                            _j.LanguageID = _languageID;
                            _j.LanguageAbilityID = _languageAbilityID;
                        }
                        _j.PositionID = _positionID;
                        _j.PositionName = _positionName;
                        _j.LocationID = _locationID;
                        _j.Number = _number;
                        _j.PeriodID = _periodID;
                        _j.SalaryID = _salaryID;
                        _j.SexID = _sexID;
                        _j.WorkingAgeID = _workingAgeID;
                        _j.JobTypeID = _jobTypeID;
                    }
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("保存职位信息出错：" + _e.Message);
            }
        }
        public static Job GetJob(Guid _id)
        {
            return DBHelper.Get<Job>(j => j.ID == _id, "获取职位信息出错:");
        }
        /// <summary>
        /// 根据职位发布者用户名获取职位列表
        /// </summary>
        /// <param name="_publisherUser">职位发布者用户名</param>
        /// <returns></returns>
        public static List<Job> GetJobList(string _publisherUser, int _jobStatus)
        {
            if (_jobStatus == 0)
            {
                return DBHelper.GetAll<Job>(j => j.PublisherUser == _publisherUser, "获取职位列表出错:").OrderBy(j => j.PublishDate).ToList();
            }
            else
            {
                return DBHelper.GetAll<Job>(j => j.PublisherUser == _publisherUser && j.StatusID == _jobStatus, "获取职位列表出错:").OrderBy(j => j.PublishDate).ToList();
            }
        }

        public static bool Delete(Guid _id)
        {
            //var _job = GetJob(_id);
            //return DBHelper.Delete(_job,"删除招聘信息出错：");
            throw new NotImplementedException();
        }

        public static List<Job> JobSearch(int _type, int _location, string _keyWord)
        {
            if (string.IsNullOrEmpty(_keyWord)) throw new Exception("关键字不能为空");
            if (!string.IsNullOrEmpty(_keyWord) && _keyWord.Length > 20) throw new Exception("关键字长度不能超过20个字符");
            if (_location == 0) throw new Exception("请选择工作地点");
            List<Job> _jobs = new List<Job>();
            using (var _sdc = DBHelper.GetDataContext())
            {
                _jobs = _sdc.Job.Where(j => j.LocationID == _location && j.Detail.Contains(_keyWord)).ToList();
            }

            return _jobs;
        }

        public static List<Job> JobSearch(int _jobType, int _position, int _industry, int _publishDateRange, int _location, string _keyWord)
        {
            if (string.IsNullOrEmpty(_keyWord)) throw new Exception("关键字不能为空");

            DateTime _publishDate = DateTime.Now.AddDays(PublishDateManager.GetPublishDateRange(_publishDateRange).Day);
            List<Job> _jobs = new List<Job>();

            using (var _sdc = DBHelper.GetDataContext())
            {
                _jobs = _sdc.Job.Where(j => j.JobTypeID == _jobType && j.PositionID == _position && j.IndustryID == _industry && j.LocationID == _location && (j.Title.Contains(_keyWord) || j.Detail.Contains(_keyWord)) && j.PublishDate < _publishDate).ToList();
            }

            return _jobs;
        }
        /// <summary>
        /// 检测职位是否过期
        /// </summary>
        /// <param name="_jobID"></param>
        /// <returns></returns>
        public static bool IsTheJobExpired(Guid _jobID)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _job = _adc.Job.Where(j => j.ID == _jobID).FirstOrDefault();
                if (_job == null) throw new Exception("检测职位是否过期出错:职位信息不存在");

                var _expired = true;

                if (_job.PublishDate.AddMonths(_job.Period.Month) > DateTime.Now)
                    _expired = false;

                return _expired;
            }
        }
        /// <summary>
        /// 检测指定用户否申请过某一工作
        /// </summary>
        /// <param name="_jobID"></param>
        /// <returns></returns>
        public static bool AmIApplyedThisJob(Guid _jobID, string _applicant)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _applyed = true;

                var _job = _adc.JobApplicationRecord.Where(r => r.JobID == _jobID && r.Applicant == _applicant).FirstOrDefault();
                if (_job == null)
                    _applyed = false;

                return _applyed;
            }
        }
    }
}
